package com.example.sikong.handler.mqtt;

import com.alibaba.fastjson.JSON;
import com.example.common.RedisKeyName;
import com.example.sikong.service.RedisProvider;
import com.example.util.SiKongMapDataUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttMessageListener;
import org.eclipse.paho.client.mqttv3.MqttMessage;

import java.util.Map;

/**
 * 鉴定
 */
@Slf4j
public class SiKongOsdMqttMessageListener implements IMqttMessageListener {

    private final RedisProvider redisProvider;

    private final TypeReference<Map<String, Object>> mapTypeReference = new TypeReference<Map<String, Object>>() {
    };

    private final long cacheActive = 30;

    public SiKongOsdMqttMessageListener(RedisProvider redisProvider) {
        this.redisProvider = redisProvider;
    }

    /**
     * 机场无人机在在线时数据
     * {
     * "OrganizationUUID":  "0c307af1-7056-440c-9455-f800e55ef59d",
     * "TopicSn":  "4TADL2L001002T",
     * "biz_code":  "device_osd",
     * "version":  "1.0",
     * "timestamp":  1708582325721,
     * "trace_id":  "",
     * "data":  {
     * "host":  {
     * "air_conditioner":  {
     * "air_conditioner_state":  0,
     * "switch_time":  0
     * },
     * "alarm_state":  0,
     * "alternate_land_point":  {
     * "height":  0,
     * "is_configured":  1,
     * "latitude":  30.470220211858976,
     * "longitude":  104.06827279444045,
     * "safe_land_height":  30
     * },
     * "battery_store_mode":  2,
     * "cover_state":  0,
     * "drone_charge_state":  {
     * "capacity_percent":  93,
     * "state":  0
     * },
     * "drone_in_dock":  1,
     * "emergency_stop_state":  0,
     * "environment_temperature":  6.8,
     * "first_power_on":  1631945855969,
     * "height":  449.9142150878906,
     * "humidity":  99,
     * "latitude":  30.470237598,
     * "longitude":  104.068179769,
     * "mode_code":  0,
     * "network_state":  {
     * "quality":  0,
     * "rate":  2.053999900817871,
     * "type":  2
     * },
     * "position_state":  {
     * "gps_number":  8,
     * "is_calibration":  1,
     * "is_fixed":  2,
     * "quality":  5,
     * "rtk_number":  31
     * },
     * "putter_state":  2,
     * "rainfall":  0,
     * "storage":  {
     * "total":  82045336,
     * "used":  51804
     * },
     * "sub_device":  {
     * "device_model_key":  "0-67-0",
     * "device_online_status":  0,
     * "device_paired":  1,
     * "device_sn":  "1581F5BMD232U001185X"
     * },
     * "supplement_light_state":  0,
     * "temperature":  11.4,
     * "wind_speed":  2
     * },
     * "sn":  "4TADL2L001002T",
     * "time_stamp":  1708582325721,
     * "timestamp":  1708582325721
     * }
     * }
     * 机场无人机不在线时数据
     * {
     * "OrganizationUUID":  "0c307af1-7056-440c-9455-f800e55ef59d",
     * "TopicSn":  "4TADL2L001002T",
     * "biz_code":  "device_osd",
     * "version":  "1.0",
     * "timestamp":  1708675815647,
     * "trace_id":  "",
     * "data":  {
     * "host":  {
     * "air_conditioner":  {
     * "air_conditioner_state":  0,
     * "switch_time":  0
     * },
     * "alarm_state":  0,
     * "alternate_land_point":  {
     * "height":  0,
     * "is_configured":  1,
     * "latitude":  30.470220211858976,
     * "longitude":  104.06827279444045,
     * "safe_land_height":  30
     * },
     * "battery_store_mode":  2,
     * "cover_state":  0,
     * "drone_charge_state":  {
     * "capacity_percent":  95,
     * "state":  0
     * },
     * "drone_in_dock":  1,
     * "emergency_stop_state":  0,
     * "environment_temperature":  4.7,
     * "first_power_on":  1631945855969,
     * "height":  449.9142150878906,
     * "humidity":  99,
     * "latitude":  30.470237598,
     * "longitude":  104.068179769,
     * "mode_code":  0,
     * "network_state":  {
     * "quality":  0,
     * "rate":  4.447999954223633,
     * "type":  2
     * },
     * "position_state":  {
     * "gps_number":  10,
     * "is_calibration":  1,
     * "is_fixed":  2,
     * "quality":  5,
     * "rtk_number":  33
     * },
     * "putter_state":  2,
     * "rainfall":  0,
     * "storage":  {
     * "total":  82045336,
     * "used":  51804
     * },
     * "sub_device":  {
     * "device_online_status":  0,
     * "device_paired":  0
     * },
     * "supplement_light_state":  0,
     * "temperature":  9.2,
     * "wind_speed":  0
     * },
     * "sn":  "4TADL2L001002T",
     * "time_stamp":  1708675815647,
     * "timestamp":  1708675815647
     * }
     * }
     * 对应上云属性地址
     * https://developer.dji.com/doc/cloud-api-tutorial/cn/api-reference/dock-to-cloud/mqtt/dock/dock1/properties.html
     * drone_in_dock
     */
    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        byte[] payload = message.getPayload();
        String payloadStr = new String(payload);
        Map<String, Object> map = JSON.parseObject(payloadStr).getInnerMap();

//        log.info("收到mqtt消息，topic:{}", topic);
//        log.info("收到mqtt消息，topic:{},payload:{}", topic, JSON.toJSONString(map));

        //  处理机库数据
        if (SiKongMapDataUtil.isHangarOsdData(map)) {
            doHangarOsdData(map);
            return;
        }
        //  处理无人机数据
        if (SiKongMapDataUtil.isDroneOsdData(map)) {
            doDroneOsdData(map);
        }
    }

    protected void doHangarOsdData(Map<String, Object> map) {
        String topicSn = SiKongMapDataUtil.getMapData("TopicSn", map);
        // 对应绑定无人机简要信息。
        Map<String, Object> sub_device = SiKongMapDataUtil.getMapData("data.host.sub_device", map);
        redisProvider.set(RedisKeyName.SI_KONG_OSD_HANGAR_ONLINE + ":" + topicSn, sub_device, cacheActive);
        Object deviceSn = SiKongMapDataUtil.getMapData("device_sn", sub_device);
        if (deviceSn != null) {
            redisProvider.set(RedisKeyName.SI_KONG_BINDING + ":" + topicSn, deviceSn, cacheActive);
            redisProvider.set(RedisKeyName.SI_KONG_BINDING + ":" + deviceSn, topicSn, cacheActive);
        }
    }

    protected void doDroneOsdData(Map<String, Object> map) {
        String topicSn = SiKongMapDataUtil.getMapData("TopicSn", map);
        // TODO 暂时存储个标志未不存具体数据
        redisProvider.set(RedisKeyName.SI_KONG_OSD_DRONE_ONLINE + ":" + topicSn, "1", cacheActive);
    }
}
